layui.config({
	base: ctx+'/plugin/authtree/',
}).extend({
	authtree: 'authtree',
}).use(['table', 'layer','form','authtree'], function(){
  var form = layui.form;
  var table = layui.table;
  var layer = layui.layer;
  var authtree = layui.authtree;
  var $ =layui.jquery;
  
  //创建角色数据列表
  var createRoleTable = function(){
	  table.render({
		  elem: '#roleFilter'
		  ,cols:  [[ 
			    {field: 'id', title: 'id',hide :true} 
			    ,{field: 'name', title: '角色名称', width: '15%',align : 'center'}
			    ,{field: 'remark', title: '备注',width: '15%',align : 'center'}
			    ,{field: 'operator', title: '操作人', width: '15%',align : 'center'}
			    ,{field: 'operateTime', title: '操作时间', width: '15%',align : 'center'}
			    ,{field: 'status', title: '状态', width: '15%',align : 'center',templet : function(row){
			    	if(row.status == 1){
			    		return "<span class='layui-badge layui-bg-blue'>激活</span>";
			    	}else if(row.status == 0){
			    		return "<span class='layui-badge layui-bg-gray'>冻结</span>";
			    	}
			    }}
			    ,{field: 'ops', title: '操作', width: '25%',align : 'center',templet : '#tool'}
		   ]]
	     ,url : ctx+"/sys/role/list"
	  });
  }
  
  createRoleTable();
  
  // 行工具条
  table.on('tool(roleFilter)',function(obj){
	  var data = obj.data; //获得当前行数据
	  var layEvent = obj.event; //获得 lay-event 对应的值
	  if(layEvent == 'changeStatus'){
		  layer.confirm('你确定要改变状态吗', function(index){
			  commons.ajax({
				  url : ctx+'/sys/role/onOrOff',
				  data : {
					  'id' : data.id
				  },
				  success : function(res){
					  layer.msg(res.msg);
					  layer.close(index);
					  table.reload('roleFilter');
				  },
				  fail : function(res){
					  layer.msg(res.msg);
					  layer.close(index);
				  }
			  });
			  
			});       
			      
	  }else if(layEvent == 'edit'){
		  edit(data);
	  }else if(layEvent == 'acledit'){
		  aclEdit(data);
	  }
  })
  
  var index ;
  // 编辑操作
  function edit(data){
	  // 回显数据
	  form.val("editForm", {
		  		'id' : data.id,
		  		'name' : data.name,
		  		'remark' : data.remark
			});
	 index =  layer.open({
		  title : '角色信息修改',
  		  type: 1,
		  content : $('#edit'),
		  area : {
			  width : '300px',
			  height : '650px'
		  }
	  });
  }
  
  // 提交更新信息
  form.on('submit(submit)',function(data){
	  commons.ajax({
		  url : ctx + '/sys/role/update',
		  data : data.field,
		  success : function(res){
			  layer.msg(res.msg);
			  table.reload('roleFilter');
			  layer.close(index);
		  },
		  fail : function(res){
			  layer.msg(res.msg);
			  layer.close(index);
		  }
	  });
	  return false;
  });
  
  // 权限维护
  function aclEdit(data){
	 // 初始化当前用户的权限树
	  commons.ajax({
			url: ctx+'/sys/roleAcl/list',
			data : {
				'id' : data.id
			},
			success: function(res){
				// 打开弹出层
	            layer.open({
		      		  title : '角色权限分配',
		        	  type: 1,
		      		  content : $('#authTree'),
		      		  area: ['500px', '500px'],
		      		  btn : ['确定','取消'],
		      		  yes: function(index, layero){
		      			var checked = authtree.getChecked('#LAY-auth-tree-index'); // 所有选中的节点内容
		      			var aclIds = '';
		      			$(checked).each(function(i,val){
		      				aclIds += val + ",";
		      			});
		      			aclIds = aclIds.substring(0,aclIds.length-1);
		      			commons.ajax({
		      				url : ctx+'/sys/roleAcl/allocate',
		      				data : {
		      					'roleId' : data.id,
		      					'aclIds' : aclIds
		      				},
		      				success : function(res){
		      					layer.msg(res.msg);
		      					layer.close(index)
		      				}
		      			});
		      		  }
		      		  ,btn2: function(index, layero){
		      			  layer.msg(res.msg);
		      			  layer.close(index);
		      		  } 
	      	  	});
	            
	             var trees = authtree.listConvert(res.data.list, {
	                primaryKey: 'id'
	                ,startPid: 0
	                ,parentKey: 'parentId'
	                ,nameKey: 'name'
	                ,valueKey: 'id'
	                ,checkedKey: res.data.checkedId
	            });
	             
	            authtree.render('#LAY-auth-tree-index', trees, {
					inputname: 'authids[]', 
					layfilter: 'lay-check-auth', 
					autowidth: true,
					openall : true
				});
	        }
	  });
	  
  }
  
});